package com.yzj.demo.common.response;

import lombok.Data;

import java.io.Serializable;

@Data
public class JsonResult<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    public JsonResult(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    /**
     *  无数据返回
     * @return JsonResult<Void>
     */
    public static JsonResult<Void> ok() {
        return new JsonResult<>(
                StatusCode.SUCCESS.getCode(),
                StatusCode.SUCCESS.getMsg(),
                null
        );
    }

    /**
     * 有数据返回
     * @param data
     * @return JsonResult<T>
     */
    public static <T> JsonResult<T> ok(T data) {
        return new JsonResult<>(
                StatusCode.SUCCESS.getCode(),
                StatusCode.SUCCESS.getMsg(),
                data
        );
    }

    /**
     *  错误返回
     * @param statusCode
     * @param data
     * @return JsonResult<T>
     */
    public static <T> JsonResult<T> error(StatusCode statusCode,T data) {
        return new JsonResult<>(
                statusCode.getCode(),
                statusCode.getMsg(),
                data
        );
    }
}
